// import { menuStore } from './stores/user';
// 权限控制
import { getProfileAPI } from './apis/user'
import router from './router'
import { getLocalToken } from './utils/auth'
import { asyncRoutes, getRoutes } from './router/asyncRoutes'
import { useUserStore } from './stores/user'

/**
 * 处理一级路由数据
 */
const getFirstRoutePerms = (permsArr: string[]) => {
  const newArr = permsArr.map(item => {
    return item.split(':')[0]
  })
  return [...new Set(newArr)]
}

// 处理二级路由权限数据
const getSecondRoutePerms = (permsArr: string[]) => {
  const newArr = permsArr.map(item => {
    const arr = item.split(':')
    return `${arr[0]}:${arr[1]}`
  })
  return [...new Set(newArr)]
}

const WHITE_LIST = ['/login', '/404']
router.beforeEach(async (to, from, next) => {
  // 2.0 开启进度条
  // NProgress.start();
  // 2.1 获取token
  const token = getLocalToken()
  // 有token
  if (token) {
    if (to.path === '/login') {
      next('/')
    } else {
      const store = useUserStore()
      if(!store.profile.id){
        const res = await getProfileAPI()
        store.setProfile(res.data)
        console.log(res.data.permissions)
        const firstRoutePerms = getFirstRoutePerms(res.data.permissions)
        console.log(firstRoutePerms)
        const secondRoutePerms = getSecondRoutePerms(res.data.permissions)
        console.log(secondRoutePerms)

        // 3. 根据权限标识 过滤筛选 动态路由表 最终得到有资格显示到左侧的所有路由表
        const perRoutes = getRoutes(firstRoutePerms, secondRoutePerms, asyncRoutes)
        console.log('最终路由表', perRoutes)

        // 4. 把动态路由表加入到路由系统中（当浏览器中访问路由的路径 显示渲染出来对应的组件）
        perRoutes.push(
          // 404 地址配置为*=》匹配不存在的地址，放到过滤动态路由规则后边
          {
            path: "/:pathMatch(.*)*",
            component: () => import("@/views/NotFound.vue"),
            meta: { hidden: true },
            children: [],
          }
        );
        perRoutes.forEach(route => {
          router.addRoute(route)
        })

        // 5. 存入Pinia渲染左侧菜单
        // const menuStore = useMenuStore()
        // menuStore.setMenuList(perRoutes)

        next({...to});
      }
      next()
    }
  } else {
    // 没有token
    if (WHITE_LIST.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})